﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TypeManager.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the TypeManager type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business
{
    using System.Collections.Generic;
    using System.Linq;
    using Common;
    using Data.Interfaces;
    using Entities;
    using Interfaces;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Business.TypeManager"/> type.
    /// </summary>
    public class TypeManager : ITypeManager
    {
        /// <summary>
        /// Account type repository
        /// </summary>
        private readonly IAccountTypeRepository _accountTypeRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="TypeManager"/> class.
        /// </summary>
        public TypeManager() :
            this(ServiceLocator.Current.GetInstance<IAccountTypeRepository>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TypeManager"/> class.
        /// </summary>
        /// <param name="accountTypeRepository">The account type repository.</param>
        public TypeManager(IAccountTypeRepository accountTypeRepository)
        {
            _accountTypeRepository = accountTypeRepository;
        }

        /// <summary>
        /// Lists the account types.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="pageNo">Page number to fetch</param>
        /// <param name="pageSize">Page size to fetch</param>
        /// <returns>List of account types matching query</returns>
        public IEnumerable<AccountType> ListAccountTypes(AccountType query = null, int? pageNo = null, int? pageSize = null)
        {
            return _accountTypeRepository.Query(query.Map<AccountType, Data.Entities.AccountType>(), pageNo, pageSize)
                .ToList()
                .Select(m => m.Map<Data.Entities.AccountType, AccountType>());
        }

        /// <summary>
        /// Gets the account type by id.
        /// </summary>
        /// <param name="id">The account type id.</param>
        /// <returns>Account type matching id</returns>
        public AccountType GetAccountTypeById(int id)
        {
            return _accountTypeRepository.FindById(id).Map<Data.Entities.AccountType, AccountType>();
        }

        /// <summary>
        /// Gets the name of the account type by.
        /// </summary>
        /// <param name="name">The name of account type</param>
        /// <returns>Account type matching id</returns>
        public AccountType GetAccountTypeByName(string name)
        {
            return _accountTypeRepository.Query(new Data.Entities.AccountType { Name = name })
                .FirstOrDefault()
                .Map<Data.Entities.AccountType, AccountType>();
        }
    }
}